package 打牢基础30天.day01.my;

/**
 * @Author yaya
 * @Create 2024/6/4 15:44
 * Description:查询符合条件最左侧位置
 */
public class Code06_BinarySearchNearLeft {

    public static int binarySearchNearLeft(int[] arr, int target){
        int left = 0;
        int right = arr.length - 1;
        int mid = 0;
        int result = -1;

        while (left < right){
            mid = left + ((right - left) >> 1);
            if (target <= arr[mid]){
                result = mid;
                right = mid - 1;
            }else {
                left = mid + 1;
            }
        }
        return result;
    }

    public static void main(String[] args) {
        int[] arr = new int[]{1,1,1,2,2,2,3,3,3,3,3,4,4,4,5,5,5,5,5,5,5,5,5,5};
        for (int i : arr) {
            System.out.println(binarySearchNearLeft(arr, i));
        }
    }
}
